package org.example.medicalstaff.controller;

import com.alibaba.fastjson.JSONObject;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.example.medicalstaff.service.PatientItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/out_doctor")
public class PatientItemController {

    @Autowired
    private PatientItemService patientItemService;

    /**
     * API1 新增检查项目
     * url格式如下：http://localhost:9090/out_doctor/project/new_check
     * 备注1：url格式说明，如果传给我一个对象的话后面可以不带?A=B这种的，之前的POST请求可能有这样的，其实是有两种传参方式，主要还是考虑这种传对象的
     * 备注2：无论State输多少，我都会给你转为1，暂存状态。
     * 备注3：还是强调这个MedicalID和RegistID和ItemID没有主外键约束，所以前端同学传值过来的时候别传错了，不然数据是不合逻辑的。
     * 测试结果：正常
     */
    @PostMapping("/project/new_check")
    public CommonResponse<String> new_check(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        String Name = (String)jsonObject.get("Name");
        String Objective = (String)jsonObject.get("Objective");
        String Position = (String)jsonObject.get("Position");
        int IsUrgent = (int)jsonObject.get("IsUrgent");
        int Num = (int)jsonObject.get("Num");
        int RecordType = (int)jsonObject.get("RecordType");
        int DoctorID = (int) jsonObject.get("DoctorID");
        int State=1;
        //插入的同一张表，只是RecordType不一样
        return patientItemService.new_check(
            MedicalID,
            RegistID,
            ItemID,
            Name,
            Objective,
            Position,
            IsUrgent,
            Num,
            State,
            RecordType,
            DoctorID
        );
    }

    /**
     * API2 新增检验项目
     * 同上，但url不一样
     */
    @PostMapping("/project/new_inspect")
    public CommonResponse<String> new_inspect(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        String Name = (String)jsonObject.get("Name");
        String Objective = (String)jsonObject.get("Objective");
        String Position = (String)jsonObject.get("Position");
        int IsUrgent = (int)jsonObject.get("IsUrgent");
        int Num = (int)jsonObject.get("Num");
        int RecordType = (int)jsonObject.get("RecordType");
        int DoctorID = (int) jsonObject.get("DoctorID");
        int State=1;
        //插入的同一张表，只是RecordType不一样
        return patientItemService.new_check(
                MedicalID,
                RegistID,
                ItemID,
                Name,
                Objective,
                Position,
                IsUrgent,
                Num,
                State,
                RecordType,
                DoctorID
        );
    }

    /**
     * API3 新增处置项目
     * 同上，但url不一样
     */
    @PostMapping("/project/new_handle")
    public CommonResponse<String> new_handle(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        String Name = (String)jsonObject.get("Name");
        String Objective = (String)jsonObject.get("Objective");
        String Position = (String)jsonObject.get("Position");
        int IsUrgent = (int)jsonObject.get("IsUrgent");
        int Num = (int)jsonObject.get("Num");
        int RecordType = (int)jsonObject.get("RecordType");
        int DoctorID = (int) jsonObject.get("DoctorID");
        int State=1;
        //插入的同一张表，只是RecordType不一样
        return patientItemService.new_check(
                MedicalID,
                RegistID,
                ItemID,
                Name,
                Objective,
                Position,
                IsUrgent,
                Num,
                State,
                RecordType,
                DoctorID
        );
    }

    /**
     * API4 根据模板和模板明细来新增检查项目
     * url格式如下：http://localhost:9090/out_doctor/project/new_check_by_template
     * 备注1：这里注意CheckTempID不要输错了，不然是找不到项目的。
     * 备注2：这个方法可能会创建多条checkapply记录，因为项目不止一个。
     * 测试结果：正常
     */
    @PostMapping("/project/new_check_by_template")
    public CommonResponse<String> new_check_by_template(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int CheckTempID = (int) jsonObject.get("CheckTempID");
        String Objective = (String)jsonObject.get("Objective");
        String Position = (String)jsonObject.get("Position");
        int IsUrgent = (int)jsonObject.get("IsUrgent");
        int Num = (int)jsonObject.get("Num");
        int RecordType = (int)jsonObject.get("RecordType");
        int DoctorID = (int) jsonObject.get("DoctorID");
        int State=1;
        //1.先根据check_temp_id获取模板明细的数量，根据数量生成对应的申请表，
        //2.然后把模板明细的项目ID和项目名称覆盖申请表中的对应信息。
        return patientItemService.new_check_by_template(
            MedicalID,
            RegistID,
            CheckTempID,
            Objective,
            Position,
            IsUrgent,
            Num,
            State,
            RecordType,
            DoctorID
        );
    }

    /**
     * API5 根据模板和模板明细来新增检验项目
     * 同上，但url不一样
     */
    @PostMapping("/project/new_inspect_by_template")
    public CommonResponse<String> new_inspect_by_template(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int CheckTempID = (int) jsonObject.get("CheckTempID");
        String Objective = (String)jsonObject.get("Objective");
        String Position = (String)jsonObject.get("Position");
        int IsUrgent = (int)jsonObject.get("IsUrgent");
        int Num = (int)jsonObject.get("Num");
        int RecordType = (int)jsonObject.get("RecordType");
        int DoctorID = (int) jsonObject.get("DoctorID");
        int State=1;
        //1.先根据check_temp_id获取模板明细的数量，根据数量生成对应的申请表，
        //2.然后把模板明细的项目ID和项目名称覆盖申请表中的对应信息。
        return patientItemService.new_check_by_template(
                MedicalID,
                RegistID,
                CheckTempID,
                Objective,
                Position,
                IsUrgent,
                Num,
                State,
                RecordType,
                DoctorID
        );
    }

    /**
     * API6 根据模板和模板明细来新增处置项目
     * 同上，但url不一样
     */
    @PostMapping("/project/new_handle_by_template")
    public CommonResponse<String> new_handle_by_template(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int CheckTempID = (int) jsonObject.get("CheckTempID");
        String Objective = (String)jsonObject.get("Objective");
        String Position = (String)jsonObject.get("Position");
        int IsUrgent = (int)jsonObject.get("IsUrgent");
        int Num = (int)jsonObject.get("Num");
        int RecordType = (int)jsonObject.get("RecordType");
        int DoctorID = (int) jsonObject.get("DoctorID");
        int State=1;
        //1.先根据check_temp_id获取模板明细的数量，根据数量生成对应的申请表，
        //2.然后把模板明细的项目ID和项目名称覆盖申请表中的对应信息。
        return patientItemService.new_check_by_template(
                MedicalID,
                RegistID,
                CheckTempID,
                Objective,
                Position,
                IsUrgent,
                Num,
                State,
                RecordType,
                DoctorID
        );
    }

    /**
     * API7 删除未开立项目
     * url格式：http://localhost:9090/out_doctor/project/del
     * 备注：无
     * 测试结果：正常
     */
    @PostMapping("/project/del")
    public CommonResponse<String> del_no_init_pro(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        System.out.println(jsonObject);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        int DoctorID = (int) jsonObject.get("DoctorID");
        return patientItemService.del_no_init_pro(
            MedicalID,
            RegistID,
            ItemID,
            DoctorID
        );
    }

    /**
     * API8 开立项目
     * url格式：http://localhost:9090/out_doctor/project/init
     * 备注：创建时间由我后端直接创建，不需要前端给参数
     * 测试结果：正常
     */
    @PutMapping("/project/init")
    public CommonResponse<String> init_pro(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        int DoctorID = (int) jsonObject.get("DoctorID");
        return patientItemService.init_pro(
            MedicalID,
            RegistID,
            ItemID,
            DoctorID
        );
    }

    /**
     * API9 作废项目
     * url格式：http://localhost:9090/out_doctor/porject/repeal
     * 备注：这个是改参数
     * 测试结果：正常
     */
    @PutMapping("/project/repeal")
    public CommonResponse<String> repeal_init_pro(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int ItemID = (int) jsonObject.get("ItemID");
        int DoctorID = (int) jsonObject.get("DoctorID");
        return patientItemService.repeal_init_pro(
                MedicalID,
                RegistID,
                ItemID,
                DoctorID
        );
    }

}
